home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / MAPPINGSTORAGE.PY < prev    next >
Encoding:
Python Source  |  1999-07-11  |  8.8 KB  |  268 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. """Very Simple Mapping ZODB storage
  65.  
  66. The Mapping storage provides an extremely simple storage
  67. implementation that doesn't provide undo or version support.
  68.  
  69. It is meant to illustrate the simplest possible storage.
  70.  
  71. The Mapping storage uses a single data structure to map
  72. object ids to data.
  73.  
  74. The Demo storage serves two purposes:
  75.  
  76.   - Provide an example implementation of a full storage without
  77.     distracting storage details,
  78.  
  79.   - Provide a volatile storage that is useful for giving demonstrations.
  80.  
  81. The demo strorage can have a "base" storage that is used in a
  82. read-only fashion. The base storage must not not to contain version
  83. data.
  84.  
  85. There are three main data structures:
  86.  
  87.   _data -- Transaction logging information necessary for undo
  88.  
  89.       This is a mapping from transaction id to transaction, where
  90.       a transaction is simply a 4-tuple:
  91.  
  92.         packed, user, description, extension_data, records
  93.  
  94.       where extension_data is a dictionary or None and records are the
  95.       actual records in chronological order. Packed is a flag
  96.       indicating whethe the transaction has been packed or not
  97.  
  98.   _index -- A mapping from oid to record
  99.  
  100.   _vindex -- A mapping from version name to version data
  101.  
  102.       where version data is a mapping from oid to record
  103.  
  104. A record is a tuple:
  105.  
  106.   oid, serial, pre, vdata, p, 
  107.  
  108. where:
  109.  
  110.      oid -- object id
  111.  
  112.      serial -- object serial number
  113.  
  114.      pre -- The previous record for this object (or None)
  115.  
  116.      vdata -- version data
  117.  
  118.         None if not a version, ortherwise:
  119.            version, non-version-record
  120.  
  121.      p -- the pickle data or None
  122.  
  123. The pickle data will be None for a record for an object created in
  124. an aborted version.
  125.  
  126. It is instructive to watch what happens to the internal data structures
  127. as changes are made.  Foe example, in Zope, you can create an external
  128. method::
  129.  
  130.   import Zope
  131.  
  132.   def info(RESPONSE):
  133.       RESPONSE['Content-type']= 'text/plain'
  134.  
  135.       return Zope.DB._storage._splat()
  136.  
  137. and call it to minotor the storage.
  138.  
  139. """
  140. __version__='$Revision: 1.3 $'[11:-2]
  141.  
  142. import POSException, BaseStorage, string, utils
  143. from TimeStamp import TimeStamp
  144.  
  145. class MappingStorage(BaseStorage.BaseStorage):
  146.  
  147.     def __init__(self, name='Mapping Storage'):
  148.  
  149.         BaseStorage.BaseStorage.__init__(self, name)
  150.  
  151.         self._index={}
  152.         self._tindex=[]
  153.  
  154.         # Note:
  155.         # If you subclass this and use a persistent mapping facility
  156.         # (e.g. a dbm file), you will need to get the maximum key and
  157.         # save it as self._oid.  See dbmStorage.
  158.  
  159.     def __len__(self):
  160.         return len(self._index)
  161.         
  162.     def getSize(self):
  163.         s=32
  164.         index=self._index
  165.         for oid in index.keys():
  166.             p=index[oid]
  167.             s=s+56+len(p)
  168.             
  169.         return s
  170.  
  171.     def load(self, oid, version):
  172.         self._lock_acquire()
  173.         try:
  174.             p=self._index[oid]
  175.             return p[8:], p[:8] # pickle, serial
  176.         finally: self._lock_release()
  177.  
  178.     def store(self, oid, serial, data, version, transaction):
  179.         if transaction is not self._transaction:
  180.             raise POSException.StorageTransactionError(self, transaction)
  181.  
  182.         if version:
  183.             raise POSException.Unsupported, "Versions aren't supported"
  184.  
  185.         self._lock_acquire()
  186.         try:
  187.             if self._index.has_key(oid):
  188.                 old=self._index[oid]
  189.                 oserial=old[:8]
  190.                 if serial != oserial: raise POSException.ConflictError
  191.                 
  192.             serial=self._serial
  193.             self._tindex.append((oid,serial+data))
  194.         finally: self._lock_release()
  195.  
  196.         return serial
  197.  
  198.     def _clear_temp(self):
  199.         self._tindex=[]
  200.  
  201.     def _finish(self, tid, user, desc, ext):
  202.  
  203.         index=self._index
  204.         for oid, p in self._tindex: index[oid]=p
  205.  
  206.     def pack(self, t, referencesf):
  207.         
  208.         self._lock_acquire()
  209.         try:    
  210.             # Build an index of *only* those objects reachable
  211.             # from the root.
  212.             index=self._index
  213.             rootl=['\0\0\0\0\0\0\0\0']
  214.             pop=rootl.pop
  215.             pindex={}
  216.             referenced=pindex.has_key
  217.             while rootl:
  218.                 oid=pop()
  219.                 if referenced(oid): continue
  220.     
  221.                 # Scan non-version pickle for references
  222.                 r=index[oid]
  223.                 pindex[oid]=r
  224.                 p=r[8:]
  225.                 referencesf(p, rootl)
  226.  
  227.             # Now delete any unreferenced entries:
  228.             for oid in index.keys():
  229.                 if not referenced(oid): del index[oid]
  230.     
  231.         finally: self._lock_release()
  232.  
  233.     def _splat(self):
  234.         """Spit out a string showing state.
  235.         """
  236.         o=[]
  237.         o.append('Index:')
  238.         index=self._index
  239.         keys=index.keys()
  240.         keys.sort()
  241.         for oid in keys:
  242.             r=index[oid]
  243.             o.append('  %s: %s, %s' %
  244.                      (utils.u64(oid),TimeStamp(r[:8]),`r[8:]`))
  245.             
  246.         return string.join(o,'\n')
  247.